package com.salim.leetcode.$67;

public class AddBinary {
    public String addBinary(String a, String b) {
        StringBuilder sb = new StringBuilder();
        int length =0;
        if(a.length()>b.length()){
            length = a.length()+1;
        }else{
            length = b.length()+1;
        }
        //是否进位
        boolean carry = false;
        int sum = 0;
        for(int i=0;i<length;i++){
            if(i<b.length()){
                sum+=(b.charAt(b.length()-1-i)-'0');
            }
            if(i<a.length()){
                sum+=(a.charAt(a.length()-1-i)-'0');
            }
            if(carry){
                sum+=1;
            }
            if(sum>1){
                carry = true;
            }else{
                carry = false;
            }
            sb.append(sum%2);
            sum = 0;
        }
        if(carry){
            sb.append(1);
        }
        sb.reverse();
        return sb.toString();
    }

    public static void main(String[] args) {
        AddBinary a = new AddBinary();
//        a.addBinary("0","0");
        a.addBinary("100","110010");
    }
}
